﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using Csla;
using Csla.Serialization;
using log4net;

namespace Sireass.Bl
{
	[Serializable]
	public class ProgramaEdit : BusinessBase<ProgramaEdit>
	{
		#region Propiedades
		// Create a logger for use in this class
		private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		public static readonly PropertyInfo<long> IdProgramaProperty = RegisterProperty<long>(c => c.IdPrograma);
		public long IdPrograma
		{
			get { return GetProperty(IdProgramaProperty); }
			private set { LoadProperty(IdProgramaProperty, value); }
		}

		public static readonly PropertyInfo<string> ClaveProgramaProperty = RegisterProperty<string>(c => c.ClavePrograma);
		[Display(Name = "Clave Programa")]
		public string ClavePrograma
		{
			get { return GetProperty(ClaveProgramaProperty); }
			set { SetProperty(ClaveProgramaProperty, value); }
		}

		public static readonly PropertyInfo<string> ProgramaProperty = RegisterProperty<string>(c => c.Programa);
		[Display(Name = "Programa")]
		public string Programa
		{
			get { return GetProperty(ProgramaProperty); }
			set { SetProperty(ProgramaProperty, value); }
		}

		public static readonly PropertyInfo<long> IdEscuelaProperty = RegisterProperty<long>(c => c.IdEscuela);
		[Display(Name = "Id Escuela")]
		public long IdEscuela
		{
			get { return GetProperty(IdEscuelaProperty); }
			set { SetProperty(IdEscuelaProperty, value); }
		}				

		public static readonly PropertyInfo<DateTime> FechaInicioVigenciaProperty = RegisterProperty<DateTime>(c => c.FechaInicioVigencia);
		[Display(Name = "Fecha Inicio Vigencia")]
		public DateTime FechaInicioVigencia
		{
			get { return GetProperty(FechaInicioVigenciaProperty); }
			set { SetProperty(FechaInicioVigenciaProperty, value); }
		}

		public static readonly PropertyInfo<DateTime> FechaFinVigenciaProperty = RegisterProperty<DateTime>(c => c.FechaFinVigencia);
		[Display(Name = "Fecha Fin Vigencia")]
		public DateTime FechaFinVigencia
		{
			get { return GetProperty(FechaFinVigenciaProperty); }
			set { SetProperty(FechaFinVigenciaProperty, value); }
		}

		public static readonly PropertyInfo<bool> HabilitadoProperty = RegisterProperty<bool>(c => c.Habilitado);
		[Display(Name = "Habilitado")]
		public bool Habilitado
		{
			get { return GetProperty(HabilitadoProperty); }
			set { SetProperty(HabilitadoProperty, value); }
		}

		public static readonly PropertyInfo<DateTime> FechaAltaProperty = RegisterProperty<DateTime>(c => c.FechaAlta);
		[Display(Name = "Fecha Alta")]
		public DateTime FechaAlta
		{
			get { return GetProperty(FechaAltaProperty); }
			set { SetProperty(FechaAltaProperty, value); }
		}

		public static readonly PropertyInfo<DateTime> FechaBajaProperty = RegisterProperty<DateTime>(c => c.FechaBaja);
		[Display(Name = "Fecha Baja")]
		public DateTime FechaBaja
		{
			get { return GetProperty(FechaBajaProperty); }
			set { SetProperty(FechaBajaProperty, value); }
		}
		#endregion

		#region Factory
		private ProgramaEdit()
		{
			//Forbidden
		}

		public static void NewProgramaEdit(EventHandler<DataPortalResult<ProgramaEdit>> callback)
		{
			DataPortal.BeginCreate<ProgramaEdit>(callback);
		}

		public static ProgramaEdit NewProgramaEdit()
		{
			return DataPortal.Create<ProgramaEdit>();
		}

		public static void GetProgramaEdit(long id, EventHandler<DataPortalResult<ProgramaEdit>> callback)
		{
			DataPortal.BeginFetch<ProgramaEdit>(id, callback);
		}

        public static ProgramaEdit GetProgramaEdit(long id)
        {
            return DataPortal.Fetch<ProgramaEdit>(id);
        }

        public static ProgramaEdit GetProgramaEdit(ProgramaCriteria criteria)
        {
            return DataPortal.Fetch<ProgramaEdit>(criteria);
        }

		public static void DeleteProgramaEdit(long id)
		{
			if (log.IsDebugEnabled)
			{
				log.Debug("DeleteProgramaEdit->id: " + id);
			}
			DataPortal.Delete<ProgramaEdit>(id);
		}
		#endregion

		#region Métodos
		public static void AddObjectAuthorizationRules()
		{
			Type type = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
			PerfilServicioCriteria criteria = new PerfilServicioCriteria(type.FullName, true);
			PerfilServicioList perfilesPorServicio = PerfilServicioList.GetPerfilServicioList(criteria);
			List<string> roles = new List<string>(perfilesPorServicio.Count);
			foreach (PerfilServicioInfo perfilServicio in perfilesPorServicio)
			{
				roles.Add(perfilServicio.Perfil);
			}
			Csla.Rules.BusinessRules.AddRule(type, new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.CreateObject, roles));
			Csla.Rules.BusinessRules.AddRule(type, new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.GetObject, roles));
			Csla.Rules.BusinessRules.AddRule(type, new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.EditObject, roles));
			Csla.Rules.BusinessRules.AddRule(type, new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.DeleteObject, roles));
		}
		#endregion

		#region Data Portal
		[RunLocal]
		protected override void DataPortal_Create()
		{
			using (BypassPropertyChecks)
			{
				IdPrograma = -1;
			}
			base.DataPortal_Create();
		}

		private void DataPortal_Fetch(long id)
		{
			using (var dalManager = Sireass.Dal.DalFactory.GetManager())
			{
				var dal = dalManager.GetProvider<Sireass.Dal.IProgramaDal>();
				using (var data = new Csla.Data.SafeDataReader(dal.Fetch(id)))
				{
					data.Read();
					using (BypassPropertyChecks)
					{
						IdPrograma = data.GetInt64("nId_Programa");
						ClavePrograma = data.GetString("cClave_Programa");
						Programa = data.GetString("cPrograma");
						IdEscuela = data.GetInt64("nId_Escuela");
						FechaInicioVigencia = data.GetDateTime("dFecha_Inicio_Vigencia");
						FechaFinVigencia = data.GetDateTime("dFechaFin_Vigencia");					
						Habilitado = data.GetBoolean("bHabilitado");
						FechaAlta = data.GetDateTime("dFecha_Alta");
						FechaBaja = data.GetDateTime("dFecha_Baja");
					}
				}
			}

            	BusinessRules.CheckRules();
		}

        private void DataPortal_Fetch(ProgramaCriteria criteria)
        {
            using (var dalManager = Sireass.Dal.DalFactory.GetManager())
            {
                var dal = dalManager.GetProvider<Sireass.Dal.IProgramaDal>();
                using (var data = new Csla.Data.SafeDataReader(dal.Fetch(criteria.IdPrograma, criteria.IdEscuela)))
                {
                    data.Read();
                    using (BypassPropertyChecks)
                    {
                       
                        IdPrograma = data.GetInt64("nId_Programa");
                        Programa = data.GetString("cPrograma");
                        ClavePrograma = data.GetString("cClave_Programa");
                        Programa = data.GetString("cPrograma");
                        Habilitado = data.GetBoolean("bHabilitado");
                        FechaAlta = data.GetDateTime("dFecha_Alta");
                        FechaBaja = data.GetDateTime("dFecha_Baja");
                    }
                }
            }
            BusinessRules.CheckRules();
        }

		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			using (var dalManager = Sireass.Dal.DalFactory.GetManager())
			{
				var dal = dalManager.GetProvider<Sireass.Dal.IProgramaDal>();
				using (BypassPropertyChecks)
				{
					IdPrograma = dal.Insert(ClavePrograma, Programa, IdEscuela, FechaInicioVigencia, FechaFinVigencia, Habilitado);
				}
			}
		}

		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			using (var dalManager = Sireass.Dal.DalFactory.GetManager())
			{
				var dal = dalManager.GetProvider<Sireass.Dal.IProgramaDal>();
				using (BypassPropertyChecks)
				{
					dal.Update(IdPrograma, ClavePrograma, Programa, IdEscuela, FechaInicioVigencia, FechaFinVigencia, Habilitado);
				}
			}
		}

		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_DeleteSelf()
		{
			using (BypassPropertyChecks)
			{
				DataPortal_Delete(IdPrograma);
			}
		}

		//[Transactional(TransactionalTypes.TransactionScope)]
		//protected override void DataPortal_DeleteSelf()
		//{
		//  using (var dalManager = Sireass.Dal.DalFactory.GetManager())
		//  {
		//    var dal = dalManager.GetProvider<Sireass.Dal.IProgramaDal>();
		//    using (BypassPropertyChecks)
		//    {
		//      dal.Delete(Id);
		//    }
		//  }
		//}

		[Transactional(TransactionalTypes.TransactionScope)]
		private void DataPortal_Delete(long id)
		{
			if (log.IsDebugEnabled)
			{
				log.Debug("DataPortal_Delete->id: " + id);
			}
			using (var dalManager = Sireass.Dal.DalFactory.GetManager())
			{
				var dal = dalManager.GetProvider<Sireass.Dal.IProgramaDal>();
				dal.Delete(id);
			}
		}

		protected override void DataPortal_OnDataPortalInvoke(DataPortalEventArgs e)
		{
			// implement your behavior here
			base.DataPortal_OnDataPortalInvoke(e);
		}

		protected override void DataPortal_OnDataPortalInvokeComplete(DataPortalEventArgs e)
		{
			// implement your behavior here
			base.DataPortal_OnDataPortalInvokeComplete(e);
		}

		protected override void Child_OnDataPortalException(DataPortalEventArgs e, Exception ex)
		{
			// implement your behavior here
			base.Child_OnDataPortalException(e, ex);
		}
		#endregion
	}
}
